#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.web.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import ${package}.web.dto.ExceptionVO;

public class ExceptionResolver extends SimpleMappingExceptionResolver
{
	private static Logger log = Logger.getLogger(ExceptionResolver.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
	{
		log.error("Exception caught on the web layer", ex);

		while (ex.getCause() != null && ex.getCause() instanceof Exception)
		{
			ex = (Exception) ex.getCause();
		}

		return super.resolveException(request, response, handler, ex);
	}

	@Override
	protected ModelAndView getModelAndView(String viewName, Exception ex)
	{
		ModelAndView mav = new ModelAndView(viewName);

		ExceptionVO exceptionVO = new ExceptionVO();
		exceptionVO.setCode(ex.getClass().getCanonicalName());
		exceptionVO.setMessage(ex.getMessage());

		mav.addObject("error", exceptionVO);

		return mav;
	}
}
